package org.akomantoso.core.io.impl;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.akomantoso.core.io.Output;

public class StaxXmlOutput implements Output{

	public static final String XMLNS = "http://www.akomantoso.org/2.0";
	
	public static final String XMLNS_INSTANCE = "http://www.w3.org/2001/XMLSchema-instance";
	
	private final XMLStreamWriter _streamWriter;
	
	public StaxXmlOutput(OutputStream outputStream) {
		try {
			_streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream, "UTF-8");
		} catch (Exception cause) {
			throw new RuntimeException(cause);
		}
		
	}
	
	/**
	 * @see org.akomantoso.core.io.Output#writeNamespace(java.lang.String, java.lang.String)
	 */
	@Override
	public void writeNamespace(String prefix, String namespaceURI) throws IOException {
		try {
			_streamWriter.writeNamespace(prefix, namespaceURI);
		}  catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}

	/**
	 * @see ec.ep.akomantoso.io.Output#writeStartDocument(java.lang.String)
	 */
	public void writeStartDocument(String rootElement) throws IOException {
		try {
			_streamWriter.writeStartDocument("UTF-8","1.1");
			_streamWriter.writeStartElement(rootElement);
			_streamWriter.writeDefaultNamespace(XMLNS);
			_streamWriter.writeNamespace("xsi",XMLNS_INSTANCE);
			_streamWriter.writeAttribute("xsi",XMLNS_INSTANCE, "schemaLocation", "http://www.akomantoso.org/2.0 akomantoso20.xsd");
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}

	/**
	 * @see ec.ep.akomantoso.io.Output#writeAttribute(java.lang.String, java.lang.String)
	 */
	public void writeAttribute(String name, String value) throws IOException {
		try {
			_streamWriter.writeAttribute(name, value);
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}


	
	/**
	 * @see ec.ep.akomantoso.io.Output#writeRaw(java.lang.String)
	 */
	public void writeRaw(String rawValue) throws IOException {
		try {
			_streamWriter.writeCharacters(rawValue);
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}	
	}


	/**
	 * @see ec.ep.akomantoso.io.Output#writeEndDocument()
	 */
	public void writeEndDocument() throws IOException {
		try {
			_streamWriter.writeEndDocument();
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}



	/**
	 * @see ec.ep.akomantoso.io.Output#writeStart(java.lang.String)
	 */
	public void writeStart(String startElement) throws IOException {
		try {
			_streamWriter.writeStartElement(startElement);
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}

	/**
	 * @see ec.ep.akomantoso.io.Output#writeEnd()
	 */
	public void writeEnd() throws IOException {
		try {
			_streamWriter.writeEndElement();
		} catch (XMLStreamException cause) {
			throw new IOException(cause);
		}
	}

	
	
	public void finish() {
		try {
			_streamWriter.writeEndDocument();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	/**
//	 * @throws XMLStreamException 
//	 * @see ec.ep.akomantoso.xml.XMLOutput#writeStartElement(java.lang.String)
//	 */
//	public void writeStartElement(String startElement) throws IOException {
//		try {
//			_streamWriter.writeStartElement(startElement);
//			_streamWriter.writeDefaultNamespace(XMLConstant.XMLNS);
//			_streamWriter.writeNamespace("xsi",XMLConstant.XMLNS_INSTANCE);
//		} catch (XMLStreamException e) {
//			e.printStackTrace();
//		}
//	}
//
//	/**
//	 * @see ec.ep.akomantoso.xml.XMLOutput#writeEndElement()
//	 */
//	public void writeEndElement() {
//		try {
//			_streamWriter.writeEndElement();
//		} catch (XMLStreamException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//	}
//
//	public void writeEndDocument() {
//		try {
//			_streamWriter.writeEndDocument();
//		} catch (XMLStreamException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
}
